package com.gitee.qdbp.general.common.web.controller.reusable;

import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gitee.qdbp.able.result.ResponseMessage;
import com.gitee.qdbp.base.annotation.OperateRecord;
import com.gitee.qdbp.base.enums.OperateType;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;

/**
 * 汉字转拼音
 *
 * @author zhaohuihua
 * @version 160222
 */
@Controller
@RequestMapping("/actions/pinyin")
@OperateRecord(value = "汉字转拼音", type = OperateType.QUERY)
public class PinYinController {

    @ResponseBody
    @RequestMapping("all")
    public ResponseMessage all(String text) {
        ResponseMessage result = new ResponseMessage();
        Map<String, String> map = new HashMap<>();
        map.put("short", PinyinHelper.getShortPinyin(text));
        map.put("full", PinyinHelper.convertToPinyinString(text, " ", PinyinFormat.WITHOUT_TONE));
        map.put("mark", PinyinHelper.convertToPinyinString(text, " ", PinyinFormat.WITH_TONE_MARK));
        result.setBody(map);
        return result;
    }

    /**
     * 简拼, 你好世界 = nhsj
     *
     * @param text
     * @return
     */
    @ResponseBody
    @RequestMapping("short")
    public ResponseMessage shortPinyin(String text) {
        ResponseMessage result = new ResponseMessage();
        result.setBody(PinyinHelper.getShortPinyin(text));
        return result;
    }

    /**
     * 全拼, 你好世界 = ni hao shi jie
     *
     * @param text
     * @return
     */
    @ResponseBody
    @RequestMapping("full")
    public ResponseMessage withoutTone(String text) {
        ResponseMessage result = new ResponseMessage();
        result.setBody(PinyinHelper.convertToPinyinString(text, " ", PinyinFormat.WITHOUT_TONE));
        return result;
    }

    /**
     * 带音标, 你好世界 = nǐ hǎo shì jiè
     *
     * @param text
     * @return
     */
    @ResponseBody
    @RequestMapping("mark")
    public ResponseMessage withToneMark(String text) {
        ResponseMessage result = new ResponseMessage();
        result.setBody(PinyinHelper.convertToPinyinString(text, " ", PinyinFormat.WITH_TONE_MARK));
        return result;
    }
}
